import sys

# 窗口配置
from PyQt5.QtWidgets import *
# 图像相关
from PyQt5.QtGui import *
# QtCore相关
from PyQt5.QtCore import *

class dataHandle(QObject):
    
    temShowSignal = pyqtSignal(str)
    humSHowSignal = pyqtSignal(str)
    lightShowSignal = pyqtSignal(str)
    smokeShowSignal = pyqtSignal(str)
    rainShowSignal = pyqtSignal(str)
    
    def __init__(self, parent = None):
        super().__init__(parent)
        
    def init_ui(self):
        pass
    
    def receiveDataVerifySlot(self, data):
        print("data = ", data)
        dataSum = 0
        length = len(data)
        
        if length < 6:
            return
        
        if data[0] == 0xAA and data[1] == 0x55:
            for i in range(5):
                dataSum += data[i]
        dataSum = dataSum & 0xFF
        # print(dataSum, data[length - 1])
        if dataSum == data[5]:
            self.receiveDataHandle(data)
    
    def receiveDataHandle(self, data):
        dataHandle = data[3:]

        if data[2] == 0x05:
            self.temperatureFunc(dataHandle)
        if data[2] == 0x06:
            self.humidnessFunc(dataHandle)
        if data[2] == 0x07:
            self.lightIntensityFunc(dataHandle)
        if data[2] == 0x08:
            self.smokeFunc(dataHandle)
        if data[2] == 0x09:
            self.rainFunc(dataHandle)
    
    def temperatureFunc(self, data):
        tem = int(data[0])   
        tem += int(data[1]) * 0.01
        # print("温度为：", tem)
        self.temShowSignal.emit(str(tem))
    
    def humidnessFunc(self, data):
        hum = int(data[0])
        # hum += int(data[1]) * 0.01
        # print("湿度为：", hum)
        self.humSHowSignal.emit(str(hum))
    
    def lightIntensityFunc(self, data):
        light = int(data[0]) / 10
        # print("光照强度为：", light)
        self.lightShowSignal.emit(str(light))
    
    def smokeFunc(self, data):
        smoke = int(data[0]) * 10
        # smoke += int(data[1]) * 0.01
        # print("烟雾浓度为：", smoke)
        self.smokeShowSignal.emit(str(smoke))
    
    def rainFunc(self, data):
        rain = data[0]
        
        if rain == 0x01:
            print("无雨")
            self.rainShowSignal.emit("今日无雨")
        elif rain == 0x02:
            print("小雨")
            self.rainShowSignal.emit("今日小雨")
        elif rain == 0x03:
            print("大雨")
            self.rainShowSignal.emit("今日大雨")